package com.haochang.chunkvod.model.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.haochang.chunkvod.app.config.ApiConfig;
import com.haochang.chunkvod.tools.thirdparty.network.http.httpenum.HttpMethodEnum;
import com.haochang.chunkvod.tools.thirdparty.network.http.httpenum.HttpRequestLoadingEnum;
import com.haochang.chunkvod.tools.thirdparty.network.http.request.HttpRequestBuilder;
import com.haochang.chunkvod.tools.thirdparty.network.http.request.HttpRequestBuilder.IHttpRequestSucessListener;
import com.haochang.chunkvod.tools.thirdparty.network.http.request.HttpRequestTask;

import android.content.Context;
import android.text.TextUtils;

/**
 * @author qintao
 *         created at 2016/6/15 11:01
 *         服务数据请求
 */

public class ServiceData
{
	private static Context context;

	public ServiceData(Context context)
	{
		this.context = context;
	}

	public interface IRequestServiceListener
	{
		public void onRequestSuccess(List<ServiceInfo> list);
	}

	/**
	 * 服务已处理or未处理列表
	 * @param room_id 房间id
	 * @param status 状态
	 * @param listener 网络请求监听
	 */
	public void requestService(String room_id, String status, final IRequestServiceListener listener)
	{
		String url = "";
		if (!TextUtils.isEmpty(room_id))
			url = "/" + room_id;
		HashMap<String, String> map = new HashMap<String, String>();
		map.put("status", status);
		HttpRequestTask httpRequestTask = new HttpRequestBuilder(context).interfaceName(ApiConfig.SERVICE + url).param(map).httpMethodEnum(HttpMethodEnum.GET)
				.httpRequestLoadingEnum(HttpRequestLoadingEnum.HTTP_LOADING_DEFAULT).httpRequestSucessListener(new IHttpRequestSucessListener()
				{

					@Override
					public void onResponseSucess(JSONObject jsonObject)
					{
						if (jsonObject != null)
						{
							List<ServiceInfo> infoList = parseServiceData(jsonObject);
							if (listener != null)
							{
								listener.onRequestSuccess(infoList);
							}
						}
					}
				}).build();
		httpRequestTask.execute();
	}

	/**
	 * 服务快捷方式列表
	 * @param roomId 房间id
	 * @param listener 网络请求监听
	 */
	public void requestServiceShortcut(String roomId, final IRequestServiceListener listener)
	{
		String url = "";
		if (!TextUtils.isEmpty(roomId))
			url = "/" + roomId;
		HashMap<String, String> map = new HashMap<String, String>();
		map.put("status", "");
		HttpRequestTask httpRequestTask = new HttpRequestBuilder(context).interfaceName(ApiConfig.SERVICE + url).param(map).httpMethodEnum(HttpMethodEnum.GET)
				.httpRequestLoadingEnum(HttpRequestLoadingEnum.HTTP_LOADING_DEFAULT).httpRequestSucessListener(new IHttpRequestSucessListener()
				{

					@Override
					public void onResponseSucess(JSONObject jsonObject)
					{
						if (jsonObject != null)
						{
							List<ServiceInfo> infoList = parseServiceShortcutData(jsonObject);
							if (listener != null)
							{
								listener.onRequestSuccess(infoList);
							}
						}
					}
				}).build();
		httpRequestTask.execute();
	}

	private List<ServiceInfo> parseServiceData(JSONObject jsonObject)
	{
		List<ServiceInfo> infoList = new ArrayList<ServiceInfo>();
		JSONArray jsonArray = jsonObject.optJSONArray("list");
		if (jsonArray != null && jsonArray.length() > 0)
		{
			for (int i = 0; i < jsonArray.length(); i++)
			{
				try
				{
					JSONObject object = jsonArray.getJSONObject(i);
					ServiceInfo info = new ServiceInfo();
					info.setService_content(object.optString("service_content"));
					info.setService_id(object.optString("service_id"));
					info.setAmount(object.optString("amount"));
					info.setCreate_time(object.optString("create_time"));
					info.setUpdate_time(object.optString("update_time"));
					infoList.add(info);
				} catch (JSONException e)
				{
					e.printStackTrace();
				}
			}
		}
		return infoList;
	}

	private List<ServiceInfo> parseServiceShortcutData(JSONObject jsonObject)
	{
		List<ServiceInfo> infoList = new ArrayList<ServiceInfo>();
		JSONArray jsonArray = jsonObject.optJSONArray("list");

		if (jsonArray != null && jsonArray.length() > 0)
		{
			for (int i = 0; i < jsonArray.length(); i++)
			{

				JSONObject object = jsonArray.optJSONObject(i);
				ServiceInfo info = new ServiceInfo();
				info.setService_content(object.optString("name"));
				info.setService_id(object.optString("service_goods_id"));
				infoList.add(info);

			}
		}
		return infoList;
	}

}
